﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace PhoneManager
{
    public partial class FrmBackup : FrmDefault
    {
        public FrmBackup()
        {
            InitializeComponent();
        }
        private void LoadFilesBkp()
        {
            DirectoryInfo Dir = new DirectoryInfo(Application.UserAppDataPath);
            FileInfo[] Files = Dir.GetFiles("PhoneManager.Backup." + SharedLib.ModemConfig.Serial + ".*.dat");
            LstFiles.Items.Clear();
            foreach (FileInfo item in Files)
            {
                LstFiles.Items.Add(item.Name);
            }
            LstFiles.SelectedIndex = -1;
        }
        private void FrmBackup_Load(object sender, EventArgs e)
        {
            LoadFilesBkp();
            int QtdSIM = 0;
            int QtdPhone = 0;
            foreach (PhoneBook item in SharedLib.PhonesBook)
            {
                if (item.SIMIndex != "")
                {
                    QtdSIM++;
                }
                if (item.PhoneIndex != "")
                {
                    QtdPhone++;
                }
            }
            LblQtdP.Text = QtdPhone.ToString();
            LblQtdS.Text = QtdSIM.ToString();
            string Name = SharedLib.ModemGSM.OperName;
            foreach (var item in Properties.Settings.Default.OperatorProviders)
            {
                if (item.Contains(Name))
                {
                    Name = item.Split(',')[1];
                    break;
                }
            }
            Name += ".";
            Name += DateTime.Now.ToString("yyyyMMdd");
            TxtNameBackup.Text = Name;
        }

        private void LstFiles_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (LstFiles.SelectedIndex == -1)
            {
                BntDel.Enabled = false;
            }
            else
            {
                BntDel.Enabled = true;
            }
        }

        private void TxtNameBackup_TextChanged(object sender, EventArgs e)
        {
            if (TxtNameBackup.Text.Trim().Length > 0)
            {
                BntBkp.Enabled = true;
            }
            else
            {
                BntBkp.Enabled = false;
            }
        }

        private void BntBkp_Click(object sender, EventArgs e)
        {
            using (FileStream fs = new FileStream(Application.UserAppDataPath + "\\PhoneManager.Backup." + SharedLib.ModemConfig.Serial + "." + TxtNameBackup.Text + ".dat", FileMode.Create))
            {
                BinaryFormatter formatter = new BinaryFormatter();
                try
                {
                    formatter.Serialize(fs, SharedLib.PhonesBook);
                }
                catch (Exception ex)
                {
                    SharedLib.MsgErro(ex.Message);
                }
            }
            LoadFilesBkp();
        }

        private void BntDel_Click(object sender, EventArgs e)
        {
            if (LstFiles.SelectedIndex == -1) return;
            FileInfo FI = new FileInfo(Application.UserAppDataPath + "\\" + LstFiles.SelectedItem.ToString());
            bool ret = SharedLib.MsgQuestion("Are you Delete file " + LstFiles.SelectedItem.ToString());
            if (ret)
            {
                try
                {
                    FI.Delete();
                    LstFiles.Items.RemoveAt(LstFiles.SelectedIndex);
                }
                catch (Exception ex)
                {
                    SharedLib.MsgErro(ex.Message);
                }
            }
        }

        private void BntCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
